package chef.myapplication;

import chef.myapplication.controller.MainController;
import chef.myapplication.model.ImageBoxes;
import chef.myapplication.model.MyAlert;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Screen;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

public class Main extends Application {
    private static Stage mainStage;
    @Override
    public void start(Stage stage) throws IOException {

        File file = new File(".\\save.TXT");
        //stage.setFullScreen(true);
        if(file.exists()){
            MainController.readObj();
        }
        Rectangle2D win= Screen.getScreens().get(0).getBounds();
        double winWidth = win.getWidth() / 1.45;
        double winHeight = win.getHeight() / 1.45;
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("MainUI.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), winWidth, winHeight);
        stage.setTitle("MyApplication");
        stage.setScene(scene);
        Main.mainStage = stage;
        setFastKey();
        stage.show();
        URL imageUrl = Main.class.getResource("icon/图片.png");

        stage.getIcons().add(new Image(Objects.requireNonNull(imageUrl).toString()));
        stage.setOnCloseRequest(event-> MainController.writeObj());
    }
    public static Stage getMainStage() {
        return mainStage;
    }

    public static void main(String[] args) {
        launch();
    }
    public void setFastKey()
    {
        KeyCombination kd = new KeyCodeCombination(KeyCode.DELETE);
        mainStage.getScene().getAccelerators().put(kd, () -> {
            if(ImageBoxes.getSelectedImageBoxes().size()==0) {
                return;
            }
            if(MyAlert.showAlert1("是否删除选中的图片？", "", Main.getMainStage()))
            {
                ImageBoxes.deleteSelectedImageBoxes();
            }
            else {
                ImageBoxes.uncheck();
            }
        });
        KeyCombination kv = new KeyCodeCombination(KeyCode.V,KeyCombination.CONTROL_DOWN);
        //其实还是当前线程执行
        mainStage.getScene().getAccelerators().put(kv, () -> {
            try {
                ImageBoxes.paste();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
        KeyCombination kc = new KeyCodeCombination(KeyCode.C,KeyCombination.CONTROL_DOWN);
        //其实还是当前线程执行
        mainStage.getScene().getAccelerators().put(kc, () -> {
            if(ImageBoxes.getSelectedImageBoxes().size()==0) {
                return;
            }
            MainController.getMainController().setFlag_Cut(false);
            ImageBoxes.clearCutImageBoxes();
            ImageBoxes.getCopiedFile().clear();
            ImageBoxes.copySelectedImageBoxes();
        });
        KeyCombination kx = new KeyCodeCombination(KeyCode.X,KeyCombination.CONTROL_DOWN);
        mainStage.getScene().getAccelerators().put(kx, () -> {
            if(ImageBoxes.getSelectedImageBoxes().size()==0) {
                return;
            }
            MainController.getMainController().setFlag_Cut(true);
            ImageBoxes.clearCutImageBoxes();
            ImageBoxes.cutImageBoxes();
        });

        KeyCombination ka = new KeyCodeCombination(KeyCode.A,KeyCombination.CONTROL_DOWN);
        mainStage.getScene().getAccelerators().put(ka, ImageBoxes::checkAll);
    }
}